#include "swmmodelname.h"

SWMModelName::SWMModelName()
{

}

/*
 *  创建模式
 */
int SWMModelName::createMode(QString name)
{
    QSqlQuery query;
    if(!query.exec(QString("select * from SWM_ModelName where ModelName='%1'").arg(name)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    else {
        if(query.first())
        {
            QMessageBox::warning(NULL, "存在相同的名称", "存在相同的模式名称，请使用其他名称。", QMessageBox::Yes, QMessageBox::Yes);
            return -4;
        }
    }
    if(!query.exec(QString("insert into SWM_ModelName(ModelName) values('%1')")\
                   .arg(name)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }

    if(!query.exec(QString("select * from SWM_ModelName where ModelName='%1'").arg(name)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    else {
        if(query.first())
        {
            int ModelID = query.value("ModelID").toInt();
            return ModelID;
        }
    }
    return -3;
}

/*
 *  获取模式列表
 */
QStringList SWMModelName::getModeList()
{
    QStringList mode;
    QSqlQuery query;
    if(!query.exec(QString("select * from SWM_ModelName")))
    {
        qWarning()<<query.lastError().text();
    }
    else {
        while (query.next()) {
            QString Modelname = query.value("ModelName").toString();
            mode.append(Modelname);
        }   
    }
    return mode;
}

/*
 *  获取模式ID
 */
int SWMModelName::getModeID(QString name)
{
    QSqlQuery query;
    if(!query.exec(QString("select * from SWM_ModelName where ModelName='%1'").arg(name)))
    {
        qWarning()<<query.lastError().text();
        return -1;
    }
    else {
        if(query.first())
        {
            int ModelID = query.value("ModelID").toInt();
            return ModelID;
        }
    }
    return -2;
}

/*
 *  获取模式name
 */
QString SWMModelName::getModeName(int id)
{
    QSqlQuery query;
    if(!query.exec(QString("select * from SWM_ModelName where ModelID='%1'").arg(id)))
    {
        qWarning()<<query.lastError().text();
    }
    else {
        if(query.first())
        {
            QString Modelname = query.value("ModelName").toString();
            return Modelname;
        }
    }
    return "标准";
}

/*
 *  获取所有模式name
 */
QStringList SWMModelName::getAllModeName()
{
    QSqlQuery query;
    QStringList allList;
    if(!query.exec(QString("select * from SWM_ModelName")))
    {
        qWarning()<<query.lastError().text();
    }
    else {
        while(query.next())
        {
            QString Modelname = query.value("ModelName").toString();
            allList.append(Modelname);
        }
    }
    return allList;
}


/*
 *  删除模式name
 */
void SWMModelName::deleteFromName(QString name)
{
    QSqlQuery query;
    if(!query.exec(QString("delete from SWM_ModelName where ModelName = '%1'").arg(name)))
    {
        qWarning()<<query.lastError().text()<<name;
    }
}
